//*******************************//
//<xml fileName="BUser.java" ?>
//<author>申鱼川</author>
//<dateTime>2014-02-23</dateTime>
//</xml>
//*******************************//
package youleche.com.business;


import java.sql.Connection;
import java.util.List;

import youleche.com.common.IDFactory;
import youleche.com.common.MD5Encrypt;
import youleche.com.connection.ConnectionFactory;
import youleche.com.factory.DaoFactory;
import youleche.com.idao.IUserDao;
import youleche.com.model.MUser;

/**
 * 用户业务类
 * 
 * @author Administrator
 * 
 */
public class BUser {
	/**
	 * 数据库连接
	 */
	private Connection con = null;
	
	/**
	 * 用户访问接口
	 */
	private IUserDao userDao = null;

	/**
	 * 构造函数
	 */
	public BUser() {
		this.userDao = DaoFactory.getDUserDao();
	}

	/**
	 * 判断用户是否登录
	 * 
	 * @param user
	 * @return 返回bool值 true登录正确
	 * @throws Exception 抛出异常
	 */
	public boolean login(MUser user) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.login(user, this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 用户注册
	 * @param user 
	 * 
	 * @param MUser
	 *            用户对象
	 * @return 注册影响的行数
	 * @throws Exception 
	 */
	public int register(MUser user) throws Exception {
		user.setUserPassword(MD5Encrypt.encryptMD5To32(user.getUserPassword()));
		user.setId(IDFactory.getId("youleche", "userInfo"));
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.register(user, this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 通过邮箱获取用户名
	 * @param email 邮箱
	 * @param con 数据库连接
	 * @return 返回用户名
	 * @throws Exception 抛出异常
	 */
	public String getUserNameByEmailAccount(String email) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getUserNameByEmailAccount(email, this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}

	/**
	 * 通过电话号码获取用户名
	 * @param phoneNum 电话号码
	 * @param con 数据库连接
	 * @return 返回用户名
	 * @throws Exception  抛出异常
	 */
	public String getUserNameByPhoneNum(String phoneNum) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getUserNameByPhoneNum(phoneNum, this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取商家总数
	 * @param con 数据库连接
	 * @return 返回商家总数
	 * @throws Exception 异常
	 */
	public int getSellerForCount() throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getSellerForCount(this.con);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 判断用户是否注册
	 * @param email 邮件地址
	 * @return 返回是否注册  true-注册
	 */
	public boolean isRegitered(String email)  throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.isRegitered(this.con, email);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 激活用户信息
	 * @param user 用户名
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int actUserInfo(MUser user) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.actUserInfo(this.con, user);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据用户名获取用户
	 * @param userName 用户名
	 * @return 用户信息
	 * @throws Exception 异常
	 */
	public MUser getUserByUserName(String userName) throws Exception{
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getUserByUserName(this.con, userName);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 更新用户密码通过用户名
	 * @param userName 用户名
	 * @return 返回影响的行数
	 * @throws Exception
	 */
	public int updatePasswordByUserName(String userName, String pass)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.updatePasswordByUserName(this.con, userName, pass);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 判断是否是商家
	 * @param email 邮件
	 * @return 返回是否是商家 true-是
	 * @throws Exception
	 */
	public boolean isSellserByEmail(String email)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.isSellserByEmail(this.con, email);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 判断是否申请了商家
	 * @param email 邮件
	 * @return 返回是否是商家 true-是
	 * @throws Exception
	 */
	public boolean isApplySellserByEmail( String email)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.isApplySellserByEmail(this.con, email);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	/**
	 * 变成商家
	 * @param email 邮件
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int becomeSellerByEmail(String email)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.becomeSellerByEmail(this.con, email);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 修改电话号码
	 * @param email 邮件
	 * @param phoneNum 电话
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int updatePhoneByEmail(String email, String phoneNum)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.updatePhoneByEmail(this.con, email, phoneNum);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 申请变成商家
	 * @param email 邮件
	 * @return 返回影响的行数
	 * @throws Exception 异常
	 */
	public int applyBecomeSellerByEmail(String email)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.applyBecomeSellerByEmail(this.con, email);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取商家列表
	 * @startIndex 数据表开始条数
	 * @endIndex 每次获取多少条数
	 * @return 返回商家列表
	 * @throws Exception 异常
	 */
	public List<MUser> getSellerList(String startIndex,
			String endIndex) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getSellerList(this.con, startIndex, endIndex);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 获取申请商家列表
	 * @startIndex 数据表开始条数
	 * @endIndex 每次获取多少条数
	 * @return 返回申请商家列表
	 * @throws Exception 异常
	 */
	public List<MUser> getApplySellerList(String startIndex,
			String endIndex) throws Exception {
		try{
				this.con = ConnectionFactory.getUlecheReadConnection();
				return this.userDao.getApplySellerList(this.con, startIndex, endIndex);
			}finally{
				if(!this.con.isClosed()){
					this.con.close();
				}
			}
		}
	/**
	 * 获取商家总数
	 * @param a4_status a4商家状态（1-商家，1.5-申请中商家）
	 * @return 返回商家总数
	 * @throws Exception 异常
	 */
	public int getSellerCountByA4_Status(String a4_status)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getSellerCountByA4_Status(this.con, a4_status);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 修改用户信息
	 * @param user 用户信息
	 * @return 返回修改影响的行数
	 * @throws Exception 异常 
	 */
	public int updateUserInfoById(MUser user) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheWriteConnection();
			return this.userDao.updateUserInfoById(this.con, user);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据用户ID得到用户信息
	 * @param id id
	 * @return 返回用户信息
	 * @throws Exception 异常
	 */
	public MUser getUserById(String id) throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getUserById(this.con, id);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
	
	/**
	 * 根据用户openID得到用户信息(openID即A10)
	 * @param openID openID
	 * @return 返回用户信息
	 * @throws Exception 异常
	 */
	public MUser getUserByOpenID_A10(String openId)
			throws Exception {
		try{
			this.con = ConnectionFactory.getUlecheReadConnection();
			return this.userDao.getUserByOpenID_A10(con, openId);
		}finally{
			if(!this.con.isClosed()){
				this.con.close();
			}
		}
	}
		
}
